<?php

namespace App\Services;

use App\Models\Game;
use App\Models\HallMenu;
use App\Models\HallMenuConfig;
use App\Models\HallMenuSub;
use App\Models\HallPartnerConfig;
use App\Models\PartnerConfig;
use common\enum\CommonEnum;
use common\enum\HallMenuEnum;
use common\enum\MasterLogEnum;
use common\utils\SysCode;
use Illuminate\Support\Facades\DB;

class HallMenuService
{

    //获取厅的H5设置列表
    public static function getH5MenuList($hall_id, $type = HallMenuEnum::HALL_MENU_TYPE_H5)
    {
        $data = HallMenu::where(['status' => CommonEnum::ENABLED, 'hall_id' => $hall_id, 'type' => $type, 'level' => 1])->get()->toArray();
        $listIds = array_column($data, 'id');
        $hallMenuConfig = HallMenuConfig::where(['hall_id' => $hall_id])->whereIn('pid', $listIds)->orderBy('pid', 'asc')->get()->toArray();
        $allpartnerName = HallPartnerConfig::where(['hall_id' => $hall_id])->get()->toArray();
        $partnerList = array_combine(array_column($allpartnerName, 'id'), array_column($allpartnerName, "partner_name"));

        $getHcGameList = Game::where(['game_type' => HallMenuEnum::GAME_ESPORT])->get()->toArray();
        $hcGame = array_combine(array_column($getHcGameList, 'id'), array_column($getHcGameList, 'name'));

        $game = [];
        foreach ($hallMenuConfig as $k => $v) {
            if ($v['game_type'] == HallMenuEnum::GAME_ESPORT && $v['level'] == 2) {
                $tmpname = isset($hcGame[$v['game_id']]) ? $hcGame[$v['game_id']] : "";
                if (!empty($tmpname)) {
                    $game[$v['pid']][] = $tmpname;
                }
            } else {
                $tmpname = isset($partnerList[$v['game_id']]) ? GameService::getPartnerFullName($partnerList[$v['game_id']],$v['game_type']) : "";
                if (!empty($tmpname)) {
                    $game[$v['pid']][] = $tmpname;
                }
            }
        }
        //兼容PC端导航二级菜单列表展示
        $hallMenuChildList = HallMenu::where(['status' => CommonEnum::ENABLED, 'hall_id' => $hall_id, 'type' => $type, 'level' => 2])->get()->toArray();
        $childMenuData = $childMenuName = [];
        foreach ($hallMenuChildList as $k => $v) {
            $childMenuData[$v['pid']][] = $v['name'];
        }

        //H5总后台列表显示热门开关游戏
        $partnerConfigHot = PartnerConfig::where(['top_hot' => CommonEnum::ENABLED, 'status' => CommonEnum::ENABLED])->get()->toArray();
        $hotList = [];
        foreach ($partnerConfigHot as $kq => $ttt) {
            $tmpname = GameService::getPartnerFullName($ttt['partner_name'],$ttt['game_type']);
            if (!empty($tmpname)) {
                $hotList[] = $tmpname;
            }
        }
        foreach ($data as $k => $va) {
            if ($va['status'] && ($va['type'] == HallMenuEnum::HALL_MENU_TYPE_H5 || $va['type'] == HallMenuEnum::HALL_MENU_TYPE_EGAME) && !$va['is_edit']) {
                $data[$k]['list_api_name'] = implode(",", $hotList);
            } else {
                $data[$k]['list_api_name'] = isset($game[$va['id']]) ? implode(",", $game[$va['id']]) : "";
            }
            $data[$k]['child_menu_list'] = isset($childMenuData[$va['id']]) ? implode(",", $childMenuData[$va['id']]) : "";
            $data[$k]['is_display_icon'] = !empty($va['is_display_icon']) ? true : false;
            $data[$k]['is_edit'] = !empty($va['is_edit']) ? true : false;
        }
        return $data;
    }

    //设置是否显示
    public static function setDisplay($data)
    {
        $ids     = array_column($data['list'], 'id');
        $oldData = HallMenu::whereIn('id', $ids)->get()->toArray();
        $oldData = array_column($oldData, null, 'id');
        $hasUpdate = false;
        foreach ($data['list'] as $key => $v) {
            if ( (isset($v['is_display']) && $v['is_display']!=$oldData[$v['id']]['is_display'])
                || (isset($v['sort']) && $v['sort'] != $oldData[$v['id']]['sort'] )
                || (isset($v['is_hot']) && $v['is_hot'] != $oldData[$v['id']]['is_hot'])
                || (isset($v['color']) && $v['color'] != $oldData[$v['id']]['color'])){

                $update = [
                    'is_display' => empty($v['is_display']) ? CommonEnum::DISABLE : CommonEnum::ENABLED,
                    'sort' => empty($v['sort']) ? 10 : $v['sort'],
                    'is_hot' => empty($v['is_hot']) ? CommonEnum::DISABLE : CommonEnum::ENABLED,
                    'is_display_icon' => empty($v['is_display_icon']) ? CommonEnum::DISABLE : CommonEnum::ENABLED,
                    'color' => !empty($v['color']) ? $v['color'] : "",
                ];
                HallMenu::where(['id' => $v['id']])->update($update);

                //更新hall_menu_sub表
                $update = [
                    'status' => in_array($update['is_display'], [CommonEnum::DISABLE,CommonEnum::DELETE])?CommonEnum::DELETE:CommonEnum::DISABLE
                ];
                HallMenuSub::where(['hall_menu_id'=>$v['id']])->update($update);
                $hasUpdate = true;
            }
        }
        if(!$hasUpdate){
            error_exit(SysCode::DATA_NO_CHANGE);
        }
        self::insertDisplayLog($oldData, $data['list']);
        return SysCode::OK;
    }

    /**
     * 导航栏保存操作日志写入
     * @param $oldData
     * @param $list
     */
    private static function insertDisplayLog($oldData, $list)
    {
        $data = [];
        $list = array_column($list, null, 'id');
        foreach ($list as $key => $value) {
            //  PC端写入old和new及remark
            if ($value['type'] == HallMenuEnum::HALL_MENU_TYPE_PC) {
                if ( (isset($value['is_display']) && $value['is_display']!=$oldData[$key]['is_display'])
                    || (isset($value['sort']) && $value['sort'] != $oldData[$key]['sort'] )
                    || (isset($value['is_hot']) && $value['is_hot'] != $oldData[$key]['is_hot'])
                    || (isset($value['color']) && $value['color'] != $oldData[$key]['color'])) {
                    $data['old'] = [
                        MasterLogEnum::MENU_TYPE_PC['is_display'] => $oldData[$key]['is_display'] ? MasterLogEnum::MENU_ENABLED : MasterLogEnum::MENU_DISABLE,
                        MasterLogEnum::MENU_TYPE_PC['sort'] => !empty($oldData[$key]['sort']) ? $oldData[$key]['sort'] : '',
                        MasterLogEnum::MENU_TYPE_PC['is_hot'] => $oldData[$key]['is_hot'] ? MasterLogEnum::MENU_ENABLED : MasterLogEnum::MENU_DISABLE,
                        MasterLogEnum::MENU_TYPE_PC['color'] => !empty($oldData[$key]['color']) ? $oldData[$key]['color'] : '',
                    ];
                    $data['new'] = [
                        MasterLogEnum::MENU_TYPE_PC['is_display'] => $value['is_display'] ? MasterLogEnum::MENU_ENABLED : MasterLogEnum::MENU_DISABLE,
                        MasterLogEnum::MENU_TYPE_PC['sort'] => !empty($value['sort']) ? $value['sort'] : '',
                        MasterLogEnum::MENU_TYPE_PC['is_hot'] => $value['is_hot'] ? MasterLogEnum::MENU_ENABLED : MasterLogEnum::MENU_DISABLE,
                        MasterLogEnum::MENU_TYPE_PC['color'] => !empty($value['color']) ? $value['color'] : '',
                    ];
                    $remark = str_replace('{:type}', MasterLogEnum::MUEN_TYPE['pc'], MasterLogEnum::REMARK['meun_edit']);
                    MasterLogService::writeMasterLog(MasterLogEnum::MEUN_EDIT, MasterLogEnum::SUCCESS, $remark, $data);
                }
            }
            // PC端电子导航栏 和 H5端  写入old和new及remark
            if (in_array($value['type'], [HallMenuEnum::HALL_MENU_TYPE_EGAME, HallMenuEnum::HALL_MENU_TYPE_H5])) {
                $menuTypeName = MasterLogEnum::MUEN_TYPE['electron'];
                if ($value['type'] == HallMenuEnum::HALL_MENU_TYPE_H5){
                    $menuTypeName = MasterLogEnum::MUEN_TYPE['h5'];
                }
                if ( (isset($value['is_display']) && $value['is_display']!=$oldData[$key]['is_display'])
                    || (isset($value['sort']) && $value['sort'] != $oldData[$key]['sort'] )) {
                    $data['old'] = [
                        MasterLogEnum::MENU_TYPE_H5['is_display'] => $oldData[$key]['is_display'] ? MasterLogEnum::MENU_ENABLED : MasterLogEnum::MENU_DISABLE,
                        MasterLogEnum::MENU_TYPE_H5['sort'] => !empty($oldData[$key]['sort']) ? $oldData[$key]['sort'] : '',
                    ];
                    $data['new'] = [
                        MasterLogEnum::MENU_TYPE_H5['is_display'] => $value['is_display'] ? MasterLogEnum::MENU_ENABLED : MasterLogEnum::MENU_DISABLE,
                        MasterLogEnum::MENU_TYPE_H5['sort'] => !empty($value['sort']) ? $value['sort'] : '',
                    ];
                    $remark = str_replace('{:type}', $menuTypeName, MasterLogEnum::REMARK['meun_edit']);
                    MasterLogService::writeMasterLog(MasterLogEnum::MEUN_EDIT, MasterLogEnum::SUCCESS, $remark, $data);
                }
            }
        }
    }

    //获取厅的所有api列表
    public static function getApiList($hall_id, $type = HallMenuEnum::HALL_MENU_TYPE_PC)
    {
        $getHcGameList = Game::where(['game_type' => HallMenuEnum::GAME_ESPORT])->get()->toArray();
        $hcGame = array_column($getHcGameList, 'name', 'id');

        $childCategoty = [];
        $hallGameConfig = PartnerService::getHallOpenPartner($hall_id, ['game_type' => $type]);
        foreach ($hallGameConfig as $k => &$vvv) {

            if ($vvv['game_type'] == HallMenuEnum::GAME_ESPORT) {
                $n = 1;
                $vvv['game_name'] = isset($vvv['partner_name']) ? GameService::getPartnerFullName($vvv['partner_name'],$vvv['game_type']) : "";
                $tmpData = [
                    'value' => $n,
                    'label' => $vvv['game_name']
                ];
                $childCategoty[] = $tmpData;
                $n++;
            } else {
                $vvv['game_name'] = GameService::getPartnerFullName($vvv['partner_name'],$vvv['game_type']);
            }
        }
        $hcGameList = [];
        if ($type == HallMenuEnum::GAME_ESPORT) {
            foreach ($hcGame as $k => $v) {
                $tmpList['id'] = $k;
                $tmpList['partner_name'] = $v;
                $tmpList['game_type'] = $type;
                $tmpList['game_name'] = $v;
                $tmpList['partner_id'] = $k;
                $hcGameList[] = $tmpList;
            }
            $hallGameConfig = $hcGameList;
        }

        $category = HallMenuEnum::GAME_NAME;
        $newCategory = [];
        foreach ($category as $kz => $vz) {
            $tmpData = [];
            $tmpData['value'] = $kz;
            $tmpData['label'] = $vz;
            $newCategory[] = $tmpData;
        }
        return ['category' => $newCategory, 'childcategory' => $childCategoty, 'gamelist' => $hallGameConfig];

    }

    //保存游戏
    public static function setApiList($params)
    {
        $oldMenu = HallMenu::where(['hall_id' => $params['hall_id'], 'id' => $params['id']])->first();
        if(empty($oldMenu)){
            error_exit(SysCode::INVALID_ARGUMENT);
        }
        $logType = MasterLogEnum::MUEN_TYPE['electron'];
        if($oldMenu['type']==HallMenuEnum::HALL_MENU_TYPE_H5){
            $logType = MasterLogEnum::MUEN_TYPE['h5'];
        }
        $remark = str_replace('{:type}', $logType, MasterLogEnum::REMARK['meun_edit']);
        $data = [];
        $data['old'][MasterLogEnum::MENU_PC_EDIT_ARRAY['game_title']] = $oldMenu['name'];
        $data['new'][MasterLogEnum::MENU_PC_EDIT_ARRAY['game_title']] = $params['name'];
        //更新对应menu的name
        HallMenu::where(['hall_id' => $params['hall_id'], 'id' => $params['id']])->update(['name' => $params['name']]);

        $oldMenuConfig = HallMenuConfig::where(['hall_id' => $params['hall_id'], 'pid' => $params['id']])->get()->toArray();
        $gameIds = $hallPartnerIds = [];
        $oldGameType = '';
        foreach ($oldMenuConfig as $v){
            $oldGameType = $v['game_type'];
            if($v['game_type']==HallMenuEnum::GAME_ESPORT && !empty($v['game_id'])){
                $gameIds[] = $v['game_id'];
            }
            if($v['game_type']!=HallMenuEnum::GAME_ESPORT && !empty($v['game_id'])){
                $hallPartnerIds[] = $v['game_id'];
            }
        }
        //删除旧数据
        $deleteModel = HallMenuConfig::where(['hall_id' => $params['hall_id'], 'pid' => $params['id']])->delete();
        $insertData = $newGameId = $newHallPartnerIds = [];
        $mewType = '';
        //编辑循环对应的游戏
        if (!empty($params['list'])) {
            foreach ($params['list'] as $key => $val) {
                $mewType = $key;
                //保存大类
                $game = explode(",", $val);
                $game  = array_unique($game);
                if (is_array($game)) {
                    foreach ($game as $ttt) {
                        if($key==HallMenuEnum::GAME_ESPORT && !empty($ttt)){
                            $newGameId[] = (int)$ttt;
                        }
                        if($key!=HallMenuEnum::GAME_ESPORT && !empty($ttt)){
                            $newHallPartnerIds[] = (int)$ttt;
                        }
                        $insertData[] = [
                            'hall_id' => $params['hall_id'],
                            'pid' => $params['id'],
                            'level' => 2,
                            'game_id' => $ttt,
                            'game_type' => $key,
                        ];
                    }
                }
            }
        }
        $gameId = array_unique(array_merge($gameIds, $newGameId));
        $games = Game::whereIn("id", $gameId)->get()->toArray();
        if(empty($games)){
            $games = [];
        }else{
            $games = array_column($games, 'name', 'id');
        }
        $hallPartnerId = array_unique(array_merge($hallPartnerIds, $newHallPartnerIds));
        $hallPartner = HallPartnerConfig::whereIn("id", $hallPartnerId)->get()->toArray();
        if(empty($hallPartner)){
            $hallPartner = [];
        }else{
            $hallPartner = array_column($hallPartner, 'partner_name', 'id');
        }
        $data['old'][MasterLogEnum::MENU_PC_EDIT_ARRAY['api_name']] = empty(HallMenuEnum::GAME_NAME[$oldGameType]) ? '' : HallMenuEnum::GAME_NAME[$oldGameType];
        $data['new'][MasterLogEnum::MENU_PC_EDIT_ARRAY['api_name']] = empty(HallMenuEnum::GAME_NAME[$mewType]) ? '' : HallMenuEnum::GAME_NAME[$mewType];
        $selected = [];
        $newSelected = [];
        if($gameIds){
            foreach ($gameIds as $v){
                $selected[] = $games[$v];
            }
        }
        if($hallPartnerIds){
            foreach ($hallPartnerIds as $v){
                $selected[] = $hallPartner[$v];
            }
        }
        if($newGameId){
            foreach ($newGameId as $v){
                $newSelected[] = $games[$v];
            }
        }
        if($newHallPartnerIds){
            foreach ($newHallPartnerIds as $v){
                $newSelected[] = $hallPartner[$v];
            }
        }
        $data['old'][MasterLogEnum::MENU_PC_EDIT_ARRAY['selected']] = implode(',', $selected);
        $data['new'][MasterLogEnum::MENU_PC_EDIT_ARRAY['selected']] = implode(',', $newSelected);
        MasterLogService::writeMasterLog(MasterLogEnum::MEUN_EDIT, MasterLogEnum::SUCCESS, $remark, $data);
        DB::table('hall_menu_config')->insert($insertData);
        return SysCode::OK;
    }


    //获取编辑页面详情数据
    public static function getEditInfoList($hall_id, $id, $type = HallMenuEnum::HALL_MENU_TYPE_PC)
    {
        $category = HallMenuEnum::GAME_NAME;
        $partnerConfig = HallPartnerConfig::where(['hall_id' => $hall_id])->get()->toArray();
        $partner_name = array_combine(array_column($partnerConfig, 'id'), array_column($partnerConfig, 'partner_name'));
        $partnerIds = array_combine(array_column($partnerConfig, 'id'), array_column($partnerConfig, 'partner_id'));
        $data = HallMenuConfig:: where(['pid' => $id, 'hall_id' => $hall_id])->get()->toArray();
        $getHcGameList = Game::where(['game_type' => HallMenuEnum::GAME_ESPORT])->get()->toArray();
        $hcGame = array_combine(array_column($getHcGameList, 'id'), array_column($getHcGameList, 'name'));
        $hcPatnerName = array_combine(array_column($getHcGameList, 'id'), array_column($getHcGameList, 'partner_name'));

        $menuDetail = HallMenu::where(['hall_id' => $hall_id, 'id' => $id])->get()->toArray();
        $result = isset($menuDetail[0]) ? $menuDetail[0] : [];
        $hcPatner = !empty($hcPatnerName) && is_array($hcPatnerName) ? reset($hcPatnerName) : [];

        foreach ($data as $k => &$v) {
            if ($v['game_type'] == HallMenuEnum::GAME_ESPORT) {
                $v['game_name'] = isset($hcGame[$v['game_id']]) ? $hcGame[$v['game_id']] : "";
                $v['partner_name'] = !empty($hcPatner) ? $hcPatner : "";
                $v['partner_id'] = isset($v['game_id']) ? $v['game_id'] : "";
            } else {
                $v['game_name'] = isset($partner_name[$v['game_id']]) ? GameService::getPartnerFullName($partner_name[$v['game_id']], $v['game_type']) : "";
                $v['partner_id'] = isset($partnerIds[$v['game_id']]) ? $partnerIds[$v['game_id']] : "";
                $v['partner_name'] = isset($partner_name[$v['game_id']]) ? $partner_name[$v['game_id']] : "";
            }
            $v['list'][$v['game_type']] = $v['game_id'];
        }
        $result['list'] = $data;
        $childMenu = HallMenu::where(['hall_id' => $hall_id, 'pid' => $id, 'status' => CommonEnum::ENABLED])->get()->toArray();

        //添加二级导航对应的api
        $childmenuListids = array_column($childMenu, 'id');
        $chileMenuRelation = HallMenuConfig:: whereIN('pid', $childmenuListids)->get()->toArray();
        foreach ($childMenu as $kiu => $bpo) {
            foreach ($chileMenuRelation as $iyt => $apo) {
                if ($bpo['id'] == $apo['pid']) {
                    $childMenu[$kiu]['list'][$apo['game_type']] = $apo['game_id'];
                    $childMenu[$kiu]['game_type'] = $apo['game_type'];
                    $childMenu[$kiu]['game_id'] = $apo['game_id'];
                    $childMenu[$kiu]['partner_id'] = $apo['game_id'];
                }
            }
        }


        $result['child_menu'] = $childMenu;
        return $result;
    }

    //新增对应的大类
    public static function addMenuData($hallId, $name, $list, $sort = 10, $display = 0)
    {
        $insertModel = [
            'hall_id' => $hallId,
            'name' => $name,
            'type' => HallMenuEnum::HALL_MENU_TYPE_EGAME,
            'sort' => $sort,
            'is_display' => $display,
        ];
        $result = new HallMenu($insertModel);
        $result->save();
        $id = $result->id;
        //编辑循环对应的游戏
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                //保存大类
                $game = explode(",", $val);
                if (is_array($game)) {
                    foreach ($game as $ttt) {
                        $insertData[] = [
                            'hall_id' => $hallId,
                            'pid' => $id,
                            'level' => 2,
                            'game_id' => $ttt,
                            'game_type' => $key,
                        ];
                    }
                }
            }
        }
        $result = DB::table('hall_menu_config')->insert($insertData);
        if (!$result) {
            error_exit(SysCode::FAILED);
        }
        $remark = str_replace(['{:type}', '{:name}'], [MasterLogEnum::MUEN_TYPE['electron'], $name], MasterLogEnum::REMARK['meun_add']);
        MasterLogService::writeMasterLog(MasterLogEnum::MEUN_ADD, MasterLogEnum::SUCCESS, $remark);
        return SysCode::OK;
    }

    //删除对应的菜单
    public static function removeData($hallId, $ids)
    {
        $ids = explode(",", $ids);
        $checkIdlist = HallMenu::whereIn('id', $ids)->where(['hall_id' => $hallId])->get()->toArray();
        //只能删除前端不显示的一级菜单
        $checkStatus = array_column($checkIdlist, 'is_display');
        foreach ($checkStatus as $k => $v) {
            if (!empty($v)) {
                return error_exit(SysCode::HALL_MENU_ISDISPLAY_STATUS_ERROR);
            }
        }
        if (count($checkIdlist) == count($ids)) {
            HallMenuConfig::whereIn('pid', $ids)->where(['hall_id' => $hallId])->delete();
            HallMenu::whereIn('id', $ids)->where(['hall_id' => $hallId])->delete();
        }
        //更新hall_menu_sub表
        $update = [
            'status' => CommonEnum::DELETE,
        ];
        HallMenuSub::whereIn('hall_menu_id', $ids)->update($update);
        //日志写入
        $remark = '';
        $pcMenu = [];
        $elecMenu = [];
        foreach ($checkIdlist as $v){
            if ($v['type'] == HallMenuEnum::HALL_MENU_TYPE_PC) {
                $pcMenu[] = $v['name'];
            }elseif($v['type'] == HallMenuEnum::HALL_MENU_TYPE_EGAME){
                $elecMenu[] = $v['name'];
            }
        }
        if ($pcMenu) {
            $remark = str_replace(['{:type}', '{:name}'], [MasterLogEnum::MUEN_TYPE['pc'], implode('、', $pcMenu)], MasterLogEnum::REMARK['meun_del']);
            MasterLogService::writeMasterLog(MasterLogEnum::MEUN_PC_DEL, MasterLogEnum::SUCCESS, $remark);
        }
        if ($elecMenu) {
            $remark = str_replace(['{:type}', '{:name}'], [MasterLogEnum::MUEN_TYPE['electron'], implode('、', $elecMenu)], MasterLogEnum::REMARK['meun_del']);
            MasterLogService::writeMasterLog(MasterLogEnum::MEUN_PC_DEL, MasterLogEnum::SUCCESS, $remark);
        }
        return $checkIdlist;
    }

    //新增一级导航
    public static function addPcMenuDataList($hallId, $list)
    {
        foreach ($list as $kk => $zzz) {
            static::addPcMenuData($hallId, $zzz['name'], $zzz['jump_switch'], $zzz['is_hot'], $zzz['list'], $zzz['color']);
        }
        $muen = [];
        foreach ($list as $value) {
            $muen[] = $value['name'];
        }
        $remark = str_replace(['{:type}', '{:name}'], [MasterLogEnum::MUEN_TYPE['pc'], implode('、', $muen)], MasterLogEnum::REMARK['meun_add']);
        MasterLogService::writeMasterLog(MasterLogEnum::MEUN_ADD, MasterLogEnum::SUCCESS, $remark);
        return SysCode::OK;
    }


    //新增对应的大类
    public static function addPcMenuData($hallId, $name, $jump_switch, $is_hot, $list, $color)
    {
        $insertModel = [
            'hall_id' => $hallId,
            'name' => $name,
            'type' => HallMenuEnum::HALL_MENU_TYPE_PC,
            'color' => $color,
            'is_hot' => !empty($is_hot) ? CommonEnum::ENABLED : CommonEnum::DISABLE,
            'jump_switch' => !empty($jump_switch) ? CommonEnum::ENABLED : CommonEnum::DISABLE,
        ];
        $result = new HallMenu($insertModel);
        $result->save();
        $id = $result->id;
        //编辑循环对应的游戏
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                //保存大类
                $game = explode(",", $val);
                if (is_array($game)) {
                    foreach ($game as $ttt) {
                        $insertData[] = [
                            'hall_id' => $hallId,
                            'pid' => $id,
                            'level' => 2,
                            'game_id' => empty($ttt) ? "" : $ttt,
                            'game_type' => $key,
                        ];
                    }
                }
            }
        }
        //添加厅主设置对应的副标题菜单名称
        $insertHallMenuSub = [
            'hall_id' => $hallId,
            'hall_menu_id' => $id,
            'hall_menu_name' => $name,
            'title' => $name,
        ];
        (new HallMenuSub($insertHallMenuSub))->save();

        DB::table('hall_menu_config')->insert($insertData);
        return SysCode::OK;
    }

    //编辑对应的大类
    public static function editPcMenuData($id, $hallId, $name, $jump_switch, $is_hot, $list, $color, $child)
    {
        //二级对应api为必填项
        if (!empty($child)) {
            foreach ($child as $kkk => $zq) {
                if (!isset($zq['list']) || !is_array($zq['list'])) {
                    error_exit(SysCode::HALL_MENU_CHILD_MENU_ERROR);
                }
                foreach ($zq['list'] as $oi => $np) {
                    if (empty($np)) {
                        error_exit(SysCode::HALL_MENU_CHILD_MENU_ERROR);
                    }
                }
            }
        }
        $update = [
            'name' => $name,
            'jump_switch' => $jump_switch,
            'color' => $color,
            'is_hot' => $is_hot,
        ];
        $data = [];
        $oldMenu = HallMenu::where(['hall_id' => $hallId, 'id' => $id])->first();
        $data['old'] = [
            MasterLogEnum::MENU_PC_EDIT_ARRAY['name'] => $oldMenu['name'],
            MasterLogEnum::MENU_PC_EDIT_ARRAY['jump_switch'] => $oldMenu['jump_switch'] ? MasterLogEnum::MENU_ENABLED :  MasterLogEnum::MENU_DISABLE,
            MasterLogEnum::MENU_PC_EDIT_ARRAY['color'] => $oldMenu['color'],
            MasterLogEnum::MENU_PC_EDIT_ARRAY['is_hot'] => $oldMenu['is_hot'] ? MasterLogEnum::MENU_ENABLED :  MasterLogEnum::MENU_DISABLE,
        ];
        $data['new'] = [
            MasterLogEnum::MENU_PC_EDIT_ARRAY['name'] => $update['name'],
            MasterLogEnum::MENU_PC_EDIT_ARRAY['jump_switch'] => $update['jump_switch'] ? MasterLogEnum::MENU_ENABLED :  MasterLogEnum::MENU_DISABLE,
            MasterLogEnum::MENU_PC_EDIT_ARRAY['color'] => $update['color'],
            MasterLogEnum::MENU_PC_EDIT_ARRAY['is_hot'] => $update['is_hot'] ? MasterLogEnum::MENU_ENABLED :  MasterLogEnum::MENU_DISABLE,
        ];
        //更新对应menu的name
        HallMenu::where(['hall_id' => $hallId, 'id' => $id])->update($update);

        //更新pc一级导航时候 更新厅的副标题
        $checkMenuSubExit = HallMenuSub::where(['hall_id'=>$hallId,'hall_menu_id'=>$id])->first();
        if($checkMenuSubExit){
            $updatehallMenuSubData = [
                'hall_menu_name'=>$name,
                'hall_id'=>$hallId,
            ];
            HallMenuSub::where(['hall_id'=>$hallId,'hall_menu_id'=>$id])->update($updatehallMenuSubData);
        }else{

            $insertHallMenuSubData = [
                'hall_menu_name'=>$name,
                'hall_menu_id'=>$id,
                'title'=>$name,
                'status'=>CommonEnum::DISABLE,
                'hall_id'=>$hallId,
            ];
            (new HallMenuSub($insertHallMenuSubData))->save();
        }
        $gameIds = $hallPartnerIds = [];
        $oldMenuConfig = HallMenuConfig::where(['hall_id' => $hallId, 'pid' => $id])->first();
        if($oldMenuConfig['game_type']==HallMenuEnum::GAME_ESPORT && !empty($oldMenuConfig['game_id'])){
            $gameIds[] = $oldMenuConfig['game_id'];
        }
        if($oldMenuConfig['game_type']!=HallMenuEnum::GAME_ESPORT && !empty($oldMenuConfig['game_id'])){
            $hallPartnerIds[] = $oldMenuConfig['game_id'];
        }
        //删除旧数据
        $deleteModel = HallMenuConfig::where(['hall_id' => $hallId, 'pid' => $id])->delete();
        //编辑循环对应的游戏
        $insertData = [];
        if (!empty($list) && is_array($list)) {
            foreach ($list as $key => $val) {
                //保存大类
                $game = explode(",", $val);
                if (is_array($game)) {
                    foreach ($game as $ttt) {
                        $insertData[] = [
                            'hall_id' => $hallId,
                            'pid' => $id,
                            'level' => 2,
                            'game_id' => $ttt,
                            'game_type' => $key,
                        ];
                        if($key==HallMenuEnum::GAME_ESPORT && !empty($ttt)){
                            $gameIds[] = $ttt;
                        }
                        if($key!=HallMenuEnum::GAME_ESPORT && !empty($ttt)){
                            $hallPartnerIds[] = $ttt;
                        }
                    }
                }
            }
        }
        //写入一级菜单保存
        if (!empty($insertData)) {
            DB::table('hall_menu_config')->insert($insertData);
        }
        //保存对应的二级菜单
        $childList = HallMenu::where(['hall_id' => $hallId, 'pid' => $id])->get()->toArray();
        $childMenu = array_column($childList, 'name', 'id');
        $childIds = array_column($childList, 'id');
        $menuConfig = HallMenuConfig::whereIn("pid", $childIds)->get()->toArray();
        foreach ($menuConfig as $v){
            if($v['game_type']==HallMenuEnum::GAME_ESPORT && !empty($v['game_id'])){
                $gameIds[] = $v['game_id'];
            }
            if($v['game_type']!=HallMenuEnum::GAME_ESPORT && !empty($v['game_id'])){
                $hallPartnerIds[] = $v['game_id'];
            }
        }

        HallMenu::whereIn("id", $childIds)->delete();
        HallMenuConfig::whereIn("pid", $childIds)->delete();
        //重新写入对应的二级菜单和api
        if (!empty($child) && is_array($child)) {
            foreach ($child as $kzz => $ppp) {
                $insertChildMenu = [
                    'hall_id' => $hallId,
                    'name' => $ppp['name'],
                    'level' => 2,
                    'pid' => $id,
                    'type' => HallMenuEnum::HALL_MENU_TYPE_PC
                ];
                $result = new HallMenu($insertChildMenu);
                $result->save();
                $childid = $result->id;
                if (!empty($ppp['list']) && is_array($ppp['list']) && count($ppp['list']) == 1) {
                    foreach ($ppp['list'] as $zzz => $ui) {
                        if($zzz==HallMenuEnum::GAME_ESPORT && !empty($ui)){
                            $gameIds[] = $ui;
                        }
                        if($zzz!=HallMenuEnum::GAME_ESPORT && !empty($ui)){
                            $hallPartnerIds[] = $ui;
                        }
                        $insertChildRelation = [
                            'hall_id' => $hallId,
                            'pid' => $childid,
                            'level' => 2,
                            'game_id' => $ui,
                            'game_type' => $zzz,
                        ];
                        $insertChildModel = new HallMenuConfig($insertChildRelation);
                        $insertChildModel->save();
                    }
                }
            }
        }
        $gameIds = array_unique($gameIds);
        $games = Game::whereIn("id", $gameIds)->get()->toArray();
        if(empty($games)){
            $games = [];
        }else{
            $games = array_column($games, 'name', 'id');
        }
        $hallPartnerIds = array_unique($hallPartnerIds);
        $hallPartner = HallPartnerConfig::whereIn("id", $hallPartnerIds)->get()->toArray();
        if(empty($hallPartner)){
            $hallPartner = [];
        }else{
            $hallPartner = array_column($hallPartner, 'partner_name', 'id');
        }
        if($oldMenuConfig['game_type']==HallMenuEnum::GAME_ESPORT && !empty($oldMenuConfig['game_id'])){
            $data['old'][MasterLogEnum::MENU_PC_EDIT_ARRAY['game']] = HallMenuEnum::GAME_NAME[$oldMenuConfig['game_type']].'/'. (empty($games[$oldMenuConfig['game_id']]) ? '' : $games[$oldMenuConfig['game_id']]);
        }
        if($oldMenuConfig['game_type']!=HallMenuEnum::GAME_ESPORT && !empty($oldMenuConfig['game_id'])){
            $data['old'][MasterLogEnum::MENU_PC_EDIT_ARRAY['game']] = HallMenuEnum::GAME_NAME[$oldMenuConfig['game_type']].'/'. (empty($hallPartner[$oldMenuConfig['game_id']]) ? '' : $hallPartner[$oldMenuConfig['game_id']]);
        }

        if (!empty($list) && is_array($list)) {
            foreach ($list as $key => $val) {
                //保存大类
                $game = explode(",", $val);
                if (is_array($game)) {
                    foreach ($game as $ttt) {
                        if($key==HallMenuEnum::GAME_ESPORT && !empty($ttt)){
                            $data['new'][MasterLogEnum::MENU_PC_EDIT_ARRAY['game']] = HallMenuEnum::GAME_NAME[$key].'/'. (empty($games[$ttt]) ? '' : $games[$ttt]);
                        }
                        if($key!=HallMenuEnum::GAME_ESPORT && !empty($ttt)){
                            $data['new'][MasterLogEnum::MENU_PC_EDIT_ARRAY['game']] = HallMenuEnum::GAME_NAME[$key].'/'. (empty($hallPartner[$ttt]) ? '' : $hallPartner[$ttt]);
                        }
                    }
                }
            }
        }
        foreach ($menuConfig as $k => $v){
            if($v['game_type']==HallMenuEnum::GAME_ESPORT){
                if (!empty($v['game_id'])){
                    $title = $childMenu[$v['pid']] .' '.MasterLogEnum::MENU_PC_EDIT_ARRAY['game_level'].':' . HallMenuEnum::GAME_NAME[$v['game_type']] .'/'. (empty($games[$v['game_id']]) ? '' : $games[$v['game_id']]);
                }else{
                    $title = $childMenu[$v['pid']] .' '.MasterLogEnum::MENU_PC_EDIT_ARRAY['game_level'].':'. HallMenuEnum::GAME_NAME[$v['game_type']];
                }
                $data['old'][($k+1).'. '.MasterLogEnum::MENU_PC_EDIT_ARRAY['name_level']] = $title;
            }
            if($v['game_type']!=HallMenuEnum::GAME_ESPORT){
                if (!empty($v['game_id'])){
                    $title = $childMenu[$v['pid']] .' '.MasterLogEnum::MENU_PC_EDIT_ARRAY['game_level'].':'.  HallMenuEnum::GAME_NAME[$v['game_type']] .'/'. (empty($hallPartner[$v['game_id']]) ? '' : $hallPartner[$v['game_id']]);
                }else{
                    $title = $childMenu[$v['pid']] .' '.MasterLogEnum::MENU_PC_EDIT_ARRAY['game_level'].':'. HallMenuEnum::GAME_NAME[$v['game_type']];
                }
                $data['old'][($k+1).'. '.MasterLogEnum::MENU_PC_EDIT_ARRAY['name_level']] = $title;
            }
        }
        if (!empty($child) && is_array($child)) {
            foreach ($child as $kzz => $ppp) {
                if (!empty($ppp['list']) && is_array($ppp['list']) && count($ppp['list']) == 1) {
                    foreach ($ppp['list'] as $zzz => $ui) {
                        if($zzz==HallMenuEnum::GAME_ESPORT){
                            if (!empty($ui)){
                                $title = $ppp['name'].' ' .MasterLogEnum::MENU_PC_EDIT_ARRAY['game_level'].':'. HallMenuEnum::GAME_NAME[$zzz] .'/'. (empty($games[$ui]) ? '' : $games[$ui]);
                            }else{
                                $title = $ppp['name'].' ' .MasterLogEnum::MENU_PC_EDIT_ARRAY['game_level'].':'. HallMenuEnum::GAME_NAME[$zzz];
                            }
                            $data['new'][($kzz+1).'. '.MasterLogEnum::MENU_PC_EDIT_ARRAY['name_level']] = $title;
                        }
                        if($zzz!=HallMenuEnum::GAME_ESPORT){
                            if (!empty($v['game_id'])){
                                $title = $ppp['name'] .' '.MasterLogEnum::MENU_PC_EDIT_ARRAY['game_level'].':'. HallMenuEnum::GAME_NAME[$zzz] .'/'. (empty($hallPartner[$ui]) ? '' : $hallPartner[$ui]);
                            }else{
                                $title = $ppp['name'] .' '.MasterLogEnum::MENU_PC_EDIT_ARRAY['game_level'].':'. HallMenuEnum::GAME_NAME[$zzz];
                            }
                            $data['new'][($kzz+1).'. '.MasterLogEnum::MENU_PC_EDIT_ARRAY['name_level']] = $title;
                        }
                    }
                }
            }
        }
        $remark = str_replace('{:type}', MasterLogEnum::MUEN_TYPE['pc'], MasterLogEnum::REMARK['meun_edit']);
        MasterLogService::writeMasterLog(MasterLogEnum::MEUN_EDIT, MasterLogEnum::SUCCESS, $remark, $data);
        return SysCode::OK;
    }


}